<template>
  <div class="log-manage">
    <el-card shadow="never">
      <div slot="header" class="card-header">
        <span>下载日志</span>
      </div>

      <!-- 查询表单 -->
      <el-form :inline="true" :model="queryForm" size="small" class="query-form">
        <el-form-item label="任务名称">
          <el-input v-model="queryForm.taskName" placeholder="请输入任务名称" clearable/>
        </el-form-item>
        <el-form-item label="执行状态">
          <el-select v-model="queryForm.executeStatus" placeholder="请选择状态" clearable>
            <el-option label="执行中" :value="0"/>
            <el-option label="成功" :value="1"/>
            <el-option label="失败" :value="2"/>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" @click="handleQuery">查询</el-button>
          <el-button icon="el-icon-refresh" @click="handleReset">重置</el-button>
        </el-form-item>
      </el-form>

      <!-- 表格 -->
      <el-table :data="tableData" border style="width: 100%" v-loading="loading">
        <el-table-column prop="id" label="ID" width="60"/>
        <el-table-column prop="taskId" label="任务ID" width="80"/>
        <el-table-column prop="taskName" label="任务名称" width="330"/>
        <el-table-column prop="startTime" label="开始时间" width="200">
          <template slot-scope="scope">
            {{ scope.row.startTime | formatDate }}
          </template>
        </el-table-column>
        <el-table-column prop="endTime" label="结束时间" width="200">
          <template slot-scope="scope">
            {{ scope.row.endTime | formatDate }}
          </template>
        </el-table-column>
        <el-table-column prop="executeStatus" label="执行状态" width="90">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.executeStatus === 0" type="info" size="small">执行中</el-tag>
            <el-tag v-else-if="scope.row.executeStatus === 1" type="success" size="small">成功</el-tag>
            <el-tag v-else type="danger" size="small">失败</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="totalCount" label="总数" width="80"/>
        <el-table-column prop="newCount" label="新增" width="80">
          <template slot-scope="scope">
            <span style="color: #67C23A; font-weight: bold">{{ scope.row.newCount }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="duplicateCount" label="重复" width="80">
          <template slot-scope="scope">
            <span style="color: #E6A23C">{{ scope.row.duplicateCount }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="errorMsg" label="错误信息" min-width="200" show-overflow-tooltip/>
        <el-table-column prop="createTime" label="创建时间" width="200">
          <template slot-scope="scope">
            {{ scope.row.createTime | formatDate }}
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <el-pagination
        class="pagination"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryForm.current"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="queryForm.size"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
import { getLogList } from '@/api/log'

export default {
  name: 'LogManage',
  data() {
    return {
      loading: false,
      queryForm: {
        current: 1,
        size: 10,
        taskName: '',
        executeStatus: null
      },
      tableData: [],
      total: 0
    }
  },
  filters: {
    formatDate(val) {
      if (!val) return '-'
      const date = new Date(val)
      return date.toLocaleString('zh-CN')
    }
  },
  mounted() {
    this.loadData()
  },
  methods: {
    loadData() {
      this.loading = true
      getLogList(this.queryForm).then(res => {
        this.tableData = res.data.records
        this.total = res.data.total
        this.loading = false
      }).catch(() => {
        this.loading = false
      })
    },
    handleQuery() {
      this.queryForm.current = 1
      this.loadData()
    },
    handleReset() {
      this.queryForm = {
        current: 1,
        size: 10,
        taskName: '',
        executeStatus: null
      }
      this.loadData()
    },
    handleSizeChange(val) {
      this.queryForm.size = val
      this.loadData()
    },
    handleCurrentChange(val) {
      this.queryForm.current = val
      this.loadData()
    }
  }
}
</script>

<style scoped>
.log-manage {
  height: 100%;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.query-form {
  margin-bottom: 20px;
}

.pagination {
  margin-top: 20px;
  text-align: right;
}
</style>

